<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JS026-日期对象</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body {
        background-color: #222;
        color: snow;
    }

    .now-time {
        margin: 1rem;
        width: 20rem;
        height: 5rem;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid snow;
    }
</style>
<body>
<div class="now-time">NOW TIME</div>
<script>
    const nowTimeEl = document.querySelector('.now-time');

    function getNowTime() {
        const date = new Date();
        const year = date.getFullYear();
        const month = date.getMonth().toString().padStart(2, '0');
        const day = date.getDate().toString().padStart(2, '0');
        const hours = date.getHours().toString().padStart(2, '0');
        const minutes = date.getMinutes().toString().padStart(2, '0');
        const seconds = date.getSeconds().toString().padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }

    function setNowTime() {
        nowTimeEl.innerText = getNowTime();
    }

    function setLocalTime() {
        nowTimeEl.innerText = new Date().toLocaleString();
    }

    setLocalTime();
    setInterval(setLocalTime, 500);
</script>
</body>
</html>